package mdFile

import (
	"editor/constant"
	"encoding/json"
	"io/ioutil"
	"sort"
)

type MDFileInfoList []*constant.MDFile

var MDFileList MDFileInfoList

func (mdList MDFileInfoList) Init() (err error) {
	MDFileList, err = GetMDFileInfoMapList()
	return
}

// Get4Admin 后台获取文件列表
func (mdList MDFileInfoList) Get4Admin() (newList []*constant.MDFile) {
	return mdList
}

// Get4Index 首页获取文件列表
func (mdList MDFileInfoList) Get4Index() (newList []*constant.MDFile) {
	for _, val := range MDFileList {
		if val.Disable == 1 {
			continue
		}

		if val.IsDir == 1 {
			var items []*constant.MDFile
			for _, n := range val.ChildList {
				if n.Disable == 1 {
					continue
				}
				items = append(items, n)
			}

			val.ChildList = items

		}

		newList = append(newList, val)
	}

	return
}

// AppendFile 添加文件
func (mdList MDFileInfoList) AppendFile(mdFile *constant.MDFile, dirName string) (err error) {
	return
}

// AppendDir 添加目录
func (mdList MDFileInfoList) AppendDir(mdDir *constant.MDFile) (err error) {
	return
}

// Save 保存到文件
func (mdList MDFileInfoList) Save() (err error) {

	return
}

// ReadConfFile 从本地读取
func (mdList MDFileInfoList) ReadConfFile() (err error) {
	return
}

// Sort 重新排序
func (mdList MDFileInfoList) Sort() MDFileInfoList {
	sort.SliceStable(mdList, func(i, j int) bool {

		if len(mdList[i].ChildList) > 0 {
			sort.SliceStable(mdList[i].ChildList, func(m, n int) bool {
				return mdList[i].ChildList[m].Sort >= mdList[i].ChildList[n].Sort
			})
		}

		if len(mdList[j].ChildList) > 0 {
			sort.SliceStable(mdList[j].ChildList, func(m, n int) bool {
				return mdList[j].ChildList[m].Sort >= mdList[j].ChildList[n].Sort
			})
		}

		return mdList[i].Sort >= mdList[j].Sort
	})

	for i, j := range mdList {
		mdList[i].Id = i + 1
		for m, _ := range j.ChildList {
			mdList[i].ChildList[m].Id = m + 1
		}
	}

	return mdList
}

func GetMDFileInfoMapList() (files []*constant.MDFile, err error) {

	var arrTemp []constant.MDFile
	filesSortInfo, _ := ioutil.ReadFile(constant.DMInfosFile)
	if filesSortInfo != nil {
		_ = json.Unmarshal(filesSortInfo, &arrTemp)
	}

	mdFiles, err := ioutil.ReadDir(constant.MDFileDir)
	if err != nil {
		return nil, err
	}

	for _, mdFile := range mdFiles {
		fileName := mdFile.Name()
		if mdFile.IsDir() {
			cnf := GetMDFileInfoByConfig(arrTemp, fileName, "")
			dirItem := constant.MDFile{
				Id:       cnf.Id,
				Name:     fileName,
				FileName: fileName,
				FileSize: 0,
				Sort:     cnf.Sort,
				ModTime:  mdFile.ModTime().Unix(),
				Disable:  cnf.Disable,
				IsDir:    1,
			}

			mdChildFiles, _ := ioutil.ReadDir(constant.MDFileDir + "/" + fileName)
			for _, child := range mdChildFiles {
				if child.IsDir() {
					continue
				}
				if child.Name()[len(child.Name())-3:] != ".md" {
					continue
				}

				cnf := GetMDFileInfoByConfig(arrTemp, fileName, fileName)

				dirItem.ChildList = append(dirItem.ChildList, &constant.MDFile{
					Id:       0,
					Name:     child.Name()[:len(child.Name())-3],
					FileName: child.Name(),
					FileSize: child.Size(),
					Sort:     cnf.Sort,
					ModTime:  child.ModTime().Unix(),
					Disable:  cnf.Disable,
				})
			}

			files = append(files, &dirItem)
		} else {
			if fileName[len(fileName)-3:] != ".md" {
				continue
			}
			cnf := GetMDFileInfoByConfig(arrTemp, fileName, "")

			files = append(files, &constant.MDFile{
				Id:       cnf.Id,
				Name:     fileName[:len(fileName)-3],
				FileName: fileName,
				FileSize: mdFile.Size(),
				Sort:     cnf.Sort,
				ModTime:  mdFile.ModTime().Unix(),
				Disable:  cnf.Disable,
				IsDir:    0,
			})
		}
	}

	if len(files) == 0 {
		files = []*constant.MDFile{
			{
				Id:       1,
				Name:     "index",
				FileName: "index.md",
				FileSize: 0,
				Sort:     0,
				ModTime:  0,
			},
		}
	}

	sort.SliceStable(files, func(i, j int) bool {

		if len(files[i].ChildList) > 0 {
			sort.SliceStable(files[i].ChildList, func(m, n int) bool {
				return files[i].ChildList[m].Sort >= files[i].ChildList[n].Sort
			})
		}

		if len(files[j].ChildList) > 0 {
			sort.SliceStable(files[j].ChildList, func(m, n int) bool {
				return files[j].ChildList[m].Sort >= files[j].ChildList[n].Sort
			})
		}

		return files[i].Sort >= files[j].Sort
	})

	for i, j := range files {
		files[i].Id = i + 1
		for m, _ := range j.ChildList {
			files[i].ChildList[m].Id = m + 1
		}
	}

	return
}
